
// PatchGeneratorDoc.cpp : implementation of the CPatchGeneratorDoc class
//

#include "stdafx.h"
#include "PatchGenerator.h"

#include "PatchGeneratorDoc.h"
#include "PatchGeneratorView.h"
#include <OpenMesh/Core/IO/MeshIO.hh>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CPatchGeneratorDoc

IMPLEMENT_DYNCREATE(CPatchGeneratorDoc, CDocument)

BEGIN_MESSAGE_MAP(CPatchGeneratorDoc, CDocument)
	ON_COMMAND(ID_FILE_OPEN, &CPatchGeneratorDoc::OnFileOpen)
	ON_COMMAND(ID_GENERATOR_INITPATCHES, &CPatchGeneratorDoc::OnGeneratorInitpatches)
	ON_COMMAND(ID_GENERATOR_UPDATE_PATCHES, &CPatchGeneratorDoc::OnGeneratorUpdatePatches)
	ON_COMMAND(ID_GENERATOR_READINITPATCHINFO, &CPatchGeneratorDoc::OnGeneratorReadinitpatchinfo)
	ON_COMMAND(ID_GENERATOR_DEBUGCORNERS, &CPatchGeneratorDoc::OnGeneratorDebugcorners)
	ON_COMMAND(ID_GENERATOR_EXTRACTPATCHINFO, &CPatchGeneratorDoc::OnGeneratorExtractpatchinfo)
	ON_COMMAND(ID_GENERATOR_PARAMETERIZATION, &CPatchGeneratorDoc::OnGeneratorParameterization)
	ON_COMMAND(ID_GENERATOR_EXPORTTAG, &CPatchGeneratorDoc::OnGeneratorExporttag)
END_MESSAGE_MAP()


// CPatchGeneratorDoc construction/destruction

CPatchGeneratorDoc::CPatchGeneratorDoc() : renderer_(mesh_), patch_generator_(mesh_)
{
	// TODO: add one-time construction code here

}

CPatchGeneratorDoc::~CPatchGeneratorDoc()
{
}

BOOL CPatchGeneratorDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}




// CPatchGeneratorDoc serialization

void CPatchGeneratorDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}


// CPatchGeneratorDoc diagnostics

#ifdef _DEBUG
void CPatchGeneratorDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CPatchGeneratorDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CPatchGeneratorDoc commands


void CPatchGeneratorDoc::OnFileOpen()
{
 CFileDialog dlg(TRUE, _T("obj"), _T("*.obj"),
    OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
    _T("Wavefront OBJ (*.obj)|*.obj|"));
  
    if (dlg.DoModal() == IDOK)
  {

	   OpenMesh::IO::Options  opt;
      CString str = dlg.GetFileName();

	  const std::string buf = std::string(CT2CA(dlg.GetPathName().GetBuffer()));

      //pModel->open(buf);

	  if ( !OpenMesh::IO::read_mesh(mesh_, std::string(CT2CA(dlg.GetPathName().GetBuffer())), opt) ){
		  
      }

	  renderer_.calc_normals();
    }

}

void CPatchGeneratorDoc::OnGeneratorInitpatches()
{
	//test
	//m_selected_vertices.clear();
	//m_selected_vertices.push_back(mesh_.vertex_handle(548));
	//m_selected_vertices.push_back(mesh_.vertex_handle(576));
	//m_selected_vertices.push_back(mesh_.vertex_handle(1050));
	//m_selected_vertices.push_back(mesh_.vertex_handle(706));
	//m_selected_vertices.push_back(mesh_.vertex_handle(54));
	//m_selected_vertices.push_back(mesh_.vertex_handle(194));
	//m_selected_vertices.push_back(mesh_.vertex_handle(159));
	//m_selected_vertices.push_back(mesh_.vertex_handle(109));

	//m_selected_vertices.clear();
	//m_selected_vertices.push_back(mesh_.vertex_handle(548));
	//m_selected_vertices.push_back(mesh_.vertex_handle(576));
	//m_selected_vertices.push_back(mesh_.vertex_handle(620));
	//m_selected_vertices.push_back(mesh_.vertex_handle(706));
	//m_selected_vertices.push_back(mesh_.vertex_handle(402));
	//m_selected_vertices.push_back(mesh_.vertex_handle(429));
	//m_selected_vertices.push_back(mesh_.vertex_handle(374));
	//m_selected_vertices.push_back(mesh_.vertex_handle(360));
	//m_selected_vertices.push_back(mesh_.vertex_handle(54));
	//m_selected_vertices.push_back(mesh_.vertex_handle(194));
	//m_selected_vertices.push_back(mesh_.vertex_handle(75));
	//m_selected_vertices.push_back(mesh_.vertex_handle(110));

	//m_selected_vertices.clear();
	//m_selected_vertices.push_back(mesh_.vertex_handle(7536));
	//m_selected_vertices.push_back(mesh_.vertex_handle(749));
	//m_selected_vertices.push_back(mesh_.vertex_handle(578));
	//m_selected_vertices.push_back(mesh_.vertex_handle(1158));
	//m_selected_vertices.push_back(mesh_.vertex_handle(5579));
	//m_selected_vertices.push_back(mesh_.vertex_handle(3787));
	//m_selected_vertices.push_back(mesh_.vertex_handle(7675));
	//m_selected_vertices.push_back(mesh_.vertex_handle(7256));
	//m_selected_vertices.push_back(mesh_.vertex_handle(2946));
	//m_selected_vertices.push_back(mesh_.vertex_handle(3278));
	//m_selected_vertices.push_back(mesh_.vertex_handle(7695));
	//m_selected_vertices.push_back(mesh_.vertex_handle(9513));

	m_selected_vertices.clear();
	m_selected_vertices.push_back(mesh_.vertex_handle(1043));
	m_selected_vertices.push_back(mesh_.vertex_handle(5029));
	m_selected_vertices.push_back(mesh_.vertex_handle(590));
	m_selected_vertices.push_back(mesh_.vertex_handle(696));
	m_selected_vertices.push_back(mesh_.vertex_handle(9380));
	m_selected_vertices.push_back(mesh_.vertex_handle(3494));
	m_selected_vertices.push_back(mesh_.vertex_handle(3126));
	m_selected_vertices.push_back(mesh_.vertex_handle(1482));
	m_selected_vertices.push_back(mesh_.vertex_handle(6344));
	m_selected_vertices.push_back(mesh_.vertex_handle(1878));
	m_selected_vertices.push_back(mesh_.vertex_handle(8503));
	m_selected_vertices.push_back(mesh_.vertex_handle(2994));
	m_selected_vertices.push_back(mesh_.vertex_handle(816));
	m_selected_vertices.push_back(mesh_.vertex_handle(20));
	m_selected_vertices.push_back(mesh_.vertex_handle(26));
	m_selected_vertices.push_back(mesh_.vertex_handle(9758));


	if(m_selected_vertices.size() < 8 || m_selected_vertices.size() % 4 != 0){
		m_selected_vertices.clear();
		return;
	}
	patch_generator_.setNSides(m_selected_vertices.size()-2);
	patch_generator_.map_onto_cube(m_selected_vertices);

	renderer_.get_cube_face_property();
	renderer_.setNSides(m_selected_vertices.size() - 2);

	UpdateAllViews(NULL);

	m_selected_vertices.clear();
}

void CPatchGeneratorDoc::drawSelectedVertices()
{
	glPushAttrib(GL_CURRENT_BIT);
	Mesh::Point pos;
	glColor3f(1.0, 0.0, 0.0);
	 glPointSize(4.0f);  
	//glDisable(GL_DEPTH_TEST);
	glBegin(GL_POINTS);
	for(int k = 0; k < m_selected_vertices.size(); k++){
		pos = mesh_.point(m_selected_vertices[k]);
		glVertex3f(pos[0], pos[1], pos[2]);
	}
	glEnd();
	glPopAttrib();
}

void CPatchGeneratorDoc::OnGeneratorUpdatePatches()
{
	// TODO: Add your command handler code here
	patch_generator_.update_patches();
	renderer_.get_cube_edge_property();

	UpdateAllViews(NULL);
}

void CPatchGeneratorDoc::OnGeneratorReadinitpatchinfo()
{
	// TODO: Add your command handler code here
	 CFileDialog dlg(TRUE, _T("obj"), _T("*.obj"),
    OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
    _T("Wavefront OBJ (*.obj)|*.obj|"));
  
    if (dlg.DoModal() == IDOK)
  {

	   OpenMesh::IO::Options  opt;
      CString str = dlg.GetFileName();

	  const std::string buf = std::string(CT2CA(dlg.GetPathName().GetBuffer()));

      //pModel->open(buf);

	  patch_generator_.read_face_side_info(std::string(CT2CA(dlg.GetPathName().GetBuffer())));
	  renderer_.get_cube_face_property();
		  
	  UpdateAllViews(NULL);
      
    }
}

void CPatchGeneratorDoc::OnGeneratorDebugcorners()
{
	// TODO: Add your command handler code here
	patch_generator_.debug_corner_vertices();

	renderer_.get_cube_face_property();

	UpdateAllViews(NULL);
}

void CPatchGeneratorDoc::OnGeneratorExtractpatchinfo()
{
	patch_generator_.init_parameterization();
	renderer_.get_edge_type_property();
	// TODO: Add your command handler code here
}

void CPatchGeneratorDoc::OnGeneratorParameterization()
{
	// TODO: Add your command handler code here
	patch_generator_.parameterization();

	UpdateAllViews(NULL);
}

void CPatchGeneratorDoc::OnGeneratorExporttag()
{
	// TODO: Add your command handler code here
	 CFileDialog dlg(FALSE, _T("tag"), _T("*.tag"),
    OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
    _T("TAG (*.tag)|*.tag|"));
  
    if (dlg.DoModal() == IDOK)
  {
    dlg.UpdateOFNFromShellDialog();


	  OutputDebugString(dlg.GetFileName());

	  patch_generator_.export_tag(std::string(CT2CA(dlg.GetPathName().GetBuffer())));
	}
}
